﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business;
using System.Configuration;
using System.Data;
using Data;

namespace WebBanDT.UserControl
{
    public partial class Login : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Page.Title = "Đăng nhập";
            txtEmail.Focus();
            if (Session["admin"] != null)
            {
                Response.Redirect("TrangChu.aspx");
            }
        }
        LayUser lu;
        protected void btnDangNhap_Click(object sender, EventArgs e)
        {
            try
            {
                string ckn = ConfigurationManager.ConnectionStrings["moketnoi"].ConnectionString;
                DataProvider.ConnectionString = ckn;
                lu = new LayUser();
                DataTable dt = new DataTable();
                dt = lu.getDSUser();
                foreach (DataRow dr in dt.Rows)
                {
                    if (dr["Email"].ToString() == txtEmail.Text && dr["MatKhau"].ToString() == txtPass.Text)
                    {
                        if (int.Parse(dr["MaKieuNguoiDung"].ToString()) == 1)
                        {
                            Session["username"] = dr["Hoten"];
                            Session["PhanQuyen"] = "admin";
                            Response.Redirect("Admin.aspx");
                            Session.Timeout = 30;
                        }
                        else
                        {
                            Session["username"] = dr["Hoten"];
                            Session["PhanQuyen"] = "User";
                            Response.Redirect("TrangChu.aspx");
                        }
                    }
                }
            }
            catch (Exception)
            {

            }

        }
    }
}